//
//  MyView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI
import Kingfisher

struct MyView: View {
    @EnvironmentObject var pathManager: PathManager
    @EnvironmentObject var themeManager: ThemeManager
    @EnvironmentObject var userManager: UserManager //加这个才能刷新
    @State var showQuitLogin = false
    
    @State private var showContactDialog = false
    var body: some View {
        ZStack{
            // 背景层
            BackgroundView(isMain: true)
            VStack {
                HStack{
                    Text("我的")
                        .font(.title)
                        .fontWeight(.bold)
                    Spacer()
                    Button((userManager.isVip()) ? "已经是Pro" : "解锁Pro") {
                        pathManager.jump(type: JumpType.VIP_VIEW)
                    }
                    .font(.system(size: 12))
                    .foregroundStyle(themeManager.themeColor)
                    .padding(10)
                    .background(themeManager.themeColor.opacity(0.1))
                    .cornerRadius(8)
                }
                //用户信息头部
                HStack(spacing:16){
                    KFImage(URL(string: userManager.getAvatar()))
                        .resizable()
                        .placeholder{
                            Image(.appIconLogo)
                                .resizable()
                                .scaledToFill()
                                .clipShape(Circle())
                        }
                        .scaledToFill()
                        .clipShape(Circle())
                        .frame(width: 80,height: 80)
                        .contentShape(Rectangle())
                        .onLongPressGesture {
                            showTipsDialog(text: "当遇到问题，提供该ID寻找客服排查问题\n用户ID: \(userManager.getUserId())", btn : "复制") {
                                UIPasteboard.general.string = userManager.getUserId()
                                showToast("复制成功")
                            }
                        }
                    VStack(alignment:.leading,spacing: 4){
                        HStack(spacing:5) {
                            Text((userManager.isLogin()) ? (userManager.getName()) : "点击登录")
                                .multilineTextAlignment(/*@START_MENU_TOKEN@*/.leading/*@END_MENU_TOKEN@*/)
                                .lineLimit(1)  // 限制文本显示1行
                                .font(.system(size: 16))
                                .foregroundStyle(.bgNormalText2)
                            if userManager.isVip() {
                                Image(.appIconVip).resizable().frame(width: 15,height: 15)
                            }
                        }
                        if userManager.isLogin() {
                            Text(userManager.getVipExpirationStr())
                                .font(.system(size: 14))
                                .multilineTextAlignment(.leading)
                                .lineLimit(1)
                                .foregroundStyle(.bgNormalText3)
                        }
                    }
                    Spacer()
                    Image(.myIconEdit).resizable().frame(width: 24,height: 24)
                    
                }.padding(EdgeInsets(top: 32, leading: 0, bottom: 0, trailing: 0))
                    .contentShape(Rectangle())
                    .onTapGesture {
                        if userManager.isLogin() {
                            //进入用户信息界面
                            pathManager.jump(type: JumpType.USER_VIEW)
                            
                        } else {
                            showLoginPopup()
                        }
                    }
                ScrollView(.vertical){
                    Spacer(minLength: 30)
                    HStack{
                        Image(.myIconNotice)
                            .resizable()
                            .renderingMode(.template)
                            .padding(5)
                            .background(themeManager.themeColor.opacity(0.1))
                            .foregroundColor(themeManager.themeColor)
                            .scaledToFit()
                            .frame(width: 50)
                            .clipShape(RoundedRectangle(cornerRadius: 15))
                        Text("公告")
                            .font(.system(size: 16))
                            .foregroundStyle(.bgNormalText2)
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                    }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                        .onTapGesture {
                            pathManager.jump(type: JumpType.NOTIFY_VIEW)
                        }
                    Spacer(minLength: 30)
//                    HStack{
//                        Image(.myIconAi)
//                            .resizable()
//                            .renderingMode(.template)
//                            .padding(9)
//                            .background(themeManager.themeColor.opacity(0.1))
//                            .foregroundColor(themeManager.themeColor)
//                            .scaledToFit()
//                            .frame(width: 50)
//                            .clipShape(RoundedRectangle(cornerRadius: 15))
//                        VStack(alignment:.leading){
//                            Text("AI生活记录伴侣")
//                                .font(.system(size: 16))
//                                .foregroundStyle(.bgNormalText2)
//                            Text("点击进入设置")
//                                .font(.system(size: 12))
//                                .foregroundStyle(.bgNormalText3)
//                        }
//                        Spacer()
//                        Image(.appIconArrowRight)
//                            .resizable()
//                            .frame(width: 15,height: 15)
//                    }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
//                        .onTapGesture {
//                            pathManager.jump(type: JumpType.AI_SET_VIEW)
//                        }
//                    Spacer(minLength: 30)
                    if !isExamine() {
                        HStack{
                            Image(.myIconHelp)
                                .resizable()
                                .renderingMode(.template)
                                .padding(8)
                                .background(themeManager.themeColor.opacity(0.1))
                                .foregroundColor(themeManager.themeColor)
                                .scaledToFit()
                                .frame(width: 50)
                                .clipShape(RoundedRectangle(cornerRadius: 15))
                            VStack(alignment:.leading){
                                Text("帮助")
                                    .font(.system(size: 16))
                                    .foregroundStyle(.bgNormalText2)
                                Text("紧急联系客服")
                                    .font(.system(size: 12))
                                    .foregroundStyle(.bgNormalText3)
                            }
                            Spacer()
                            Image(.appIconArrowRight)
                                .resizable()
                                .frame(width: 15,height: 15)
                        }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                            .onTapGesture {
                                showContactDialog.toggle()
                            }.confirmationDialog("联系我们", isPresented: $showContactDialog){
                                Button("微信"){
                                    contactWx()
                                }
                                Button("QQ"){
                                    contactQQ()
                                }
                                Button("QQ群"){
                                    contactQQGroup(groupUin: configManager.getConfig().qqGroup?.qqGroup ?? "", key: configManager.getConfig().qqGroup?.qqGroupKeyIos ?? "")
                                }
                                Button("取消", role: .cancel){}
                            } message: {
                                Text("请选择联系方式")
                            }
                        Spacer(minLength: 30)
                    }
                    HStack{
                        Image(.myIconFeedback)
                            .resizable()
                            .renderingMode(.template)
                            .padding(8)
                            .background(themeManager.themeColor.opacity(0.1))
                            .foregroundColor(themeManager.themeColor)
                            .scaledToFit()
                            .frame(width: 50)
                            .clipShape(RoundedRectangle(cornerRadius: 15))
                        Text("意见反馈")
                            .font(.system(size: 16))
                            .foregroundStyle(.bgNormalText2)
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                    }.contentShape(Rectangle())
                        .onTapGesture {
                            pathManager.jump(type: JumpType.FEEDBACK)
                        }
                    Spacer(minLength: 30)
                    HStack{
                        Image(.myIconBubble)
                            .resizable()
                            .renderingMode(.template)
                            .padding(8)
                            .background(themeManager.themeColor.opacity(0.1))
                            .foregroundColor(themeManager.themeColor)
                            .scaledToFit()
                            .frame(width: 50)
                            .clipShape(RoundedRectangle(cornerRadius: 15))
                        Text("气泡商城")
                            .font(.system(size: 16))
                            .foregroundStyle(.bgNormalText2)
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                    }.contentShape(Rectangle())
                        .onTapGesture {
                            pathManager.jump(type: JumpType.BUBBLE_SHOP)
                        }
                    Spacer(minLength: 30)
                    HStack{
                        Image(.myIconSet)
                            .resizable()
                            .renderingMode(.template)
                            .padding(2)
                            .background(themeManager.themeColor.opacity(0.1))
                            .foregroundColor(themeManager.themeColor)
                            .scaledToFit()
                            .frame(width: 50)
                            .clipShape(RoundedRectangle(cornerRadius: 15))
                        Text("设置")
                            .font(.system(size: 16))
                            .foregroundStyle(.bgNormalText2)
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                    }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                        .onTapGesture {
                            pathManager.jump(type: JumpType.SET_VIEW)
                        }
                    Spacer(minLength: 30)
                    if userManager.isLogin() {
                        Button(action: {
                            showQuitLogin.toggle()
                        }) {
                            Text("退出登陆")
                                .font(.system(size: 16))
                                .foregroundStyle(themeManager.themeColor)
                        }
                    }
                }
                
            }.padding(EdgeInsets(top: 0, leading: 30, bottom: 0, trailing: 20))
        }.background(.bg)
            .alert("温馨提示", isPresented: $showQuitLogin){
                Button("取消",role: .cancel){}
                Button("确定",role: .destructive) {
                    userManager.doLogOut()
                }
            } message: {
                Text("是否退出登陆")
            }
        
    }
}



//#Preview {
//    MyView()
//}

//定义预览提供者
struct My_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: MyView())
    }
}
